package com.panshi.controller.article;

import com.panshi.api.ArticleApi;
import com.panshi.api.SortApi;
import com.panshi.pagevo.PageBeanVo;
import com.panshi.po.ArticlePo;
import com.panshi.po.SortPO;
import com.panshi.po.UserPO;
import com.panshi.vo.ArticleSortNameQueryVo;
import com.panshi.vo.ArticleStateQueryVo;
import com.panshi.vo.ArticleTitleQueryVo;
import com.panshi.vo.PageModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.naming.ldap.PagedResultsControl;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文章信息
 */
@Controller
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleApi articleApi;

    @Resource
    private SortApi sortApi;


    /**
     * 跳转页面
     * @param request
     * @return
     */
    @GetMapping("queryess")
    public String query(HttpServletRequest request){

        List<SortPO> list=  sortApi.totalQuery().getData();
        log.info("listquerySortname==========={}",list);
        request.setAttribute("list",list);

        return "article/article-query";
    }

    /**
     * 查询文章所有信息
     * @param ps
     * @return
     */
    @PostMapping("article-queryall")
    @ResponseBody
    public PageModel<ArticlePo> queryall(PageModel<ArticlePo> ps){
        ps.init();
        PageBeanVo pageBeanVo=new PageBeanVo();
        pageBeanVo.setCurrentPage(ps.getCurrentPage());
        pageBeanVo.setPageSize(ps.getPageSize());

      List<ArticlePo> list=articleApi.queryAll(pageBeanVo).getData();
     Integer integer=   articleApi.articleCount().getData();
      log.info("articlelist========={}",list);

      ps.setCount(integer);
        ps.setList(list);

        return ps;
    }

    /**
     * 根据标题模糊查询文章信息
     * @param ps
     * @param title
     * @return
     */
    @PostMapping("article-queryDim/{title}")
    @ResponseBody
    public PageModel<ArticlePo>  queryDim(PageModel<ArticlePo> ps, @PathVariable String title){

        ArticleTitleQueryVo articleTitleQueryVo=new ArticleTitleQueryVo();

        articleTitleQueryVo.setTitle(title);
        articleTitleQueryVo.setCurrentPage(ps.getCurrentPage());
        articleTitleQueryVo.setPageSize(ps.getPageSize());
        List<ArticlePo> list=  articleApi.titleQuery(articleTitleQueryVo).getData();
       Integer integer= articleApi.titleCount(title).getData();
        log.info("listqueryDim==========={}",list);

        ps.setCount(integer);
        ps.setList(list);

        return ps;
    }

    /**
     * 根据状态查询文章信息
     * @param ps
     * @param state
     * @return
     */
    @PostMapping("article-queryState/{state}")
    @ResponseBody
    public  PageModel<ArticlePo> queryState(PageModel<ArticlePo> ps,@PathVariable Integer  state){
        ps.init();
        ArticleStateQueryVo articleStateQueryVo=new ArticleStateQueryVo();
        articleStateQueryVo.setState(state);
        articleStateQueryVo.setCurrentPage(ps.getCurrentPage());
        articleStateQueryVo.setPageSize(ps.getPageSize());

      List<ArticlePo>  list=articleApi.stateQuery(articleStateQueryVo).getData();
      log.info("listqueryState==========={}",list);
        Integer integer=  articleApi.stateCount(state).getData();
      ps.setList(list);
        ps.setCount(integer);
      return ps;
    }


    /**
     * 根据文章分类名称查询文章信息
     * @param ps
     * @param sortname
     * @return
     */
    @PostMapping("article-querySortname/{sortname}")
    @ResponseBody
    public  PageModel<ArticlePo> querySortname(PageModel<ArticlePo> ps, @PathVariable String sortname){
        ps.init();
        ArticleSortNameQueryVo articleSortNameQueryVo=new ArticleSortNameQueryVo();
        articleSortNameQueryVo.setSortname(sortname);
        articleSortNameQueryVo.setCurrentPage(ps.getCurrentPage());
        articleSortNameQueryVo.setPageSize(ps.getPageSize());

      List<ArticlePo>  list=articleApi.sortNameQuery(articleSortNameQueryVo).getData();
        Integer integer=articleApi.sortNameCounts(sortname).getData();
        ps.setList(list);
        ps.setCount(integer);
        return ps;
    }

}
